<?php
namespace PsiFramework\Classes\Util\HTTP;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

use \PsiFramework\Classes\Util\Object\File;

import('Classes.Util.Object.File');

/**
 * The Request class represents the requests
 * passed by the navigator (GET, POST and FILE)
 *
 * @package Util.HTTP
 */
class Request
{

    protected /*array*/ $_post ;
    protected /*array*/ $_get ;
    protected /*array*/ $_file ;
    protected /*array*/ $_server ;

    protected static /*Request*/ $_singleton ;

    protected function __construct()
    {
        $this->_post = $_POST ;
        $this->_get = $_GET ;
        $this->_file = $_FILES ;
        $this->_server = $_SERVER ;
    }

    /**
     * Removes the magic quotes if necessary
     *
     * @param       mixed   $parameter      the parameter
     * @param       bool    $string         is the parameter a string
     * @return      mixed   the parameter
     */
    protected function removeMagicQuotes(
        /*mixed*/ $parameter,
        /*bool*/ $string=true
    )
    {
        //if the server has the magic quotes activated
        //and the request parameter is not null
        if (get_magic_quotes_gpc() && $parameter!=null) {
            //if the parameter is a string
            if ($string) {
                $parameter = stripslashes($parameter);
            }
            //if it's ann array
            else{
                $newParam = array();
                foreach ($parameter as $paramValue) {
                    $newParam[] = stripslashes($paramValue);
                }
                $parameter = $newParam ;
            }
        }

        return $parameter ;
    }

    /**
     * This function allows to retrieve a parameter matching the given name
     *
     * @param  string  $paramName    the name of the parameter
     * @param  boolean $string       is the variable a string or an array
     * @return mixed   the parameter
     */
    public function getParameter(/*string*/ $paramName, /*bool*/ $string=true)
    {
        $return = null ;

        //we try the get variable
        if (isset($this->_get[$paramName])) {
            $return = $this->removeMagicQuotes(urldecode($this->_get[$paramName]));
        } else if (isset($this->_post[$paramName])) {
            //then we try to retrieve the post variable
            $return = $this->removeMagicQuotes($this->_post[$paramName]);
        }
        
        return $return;
    }

    /**
     * This function returns the specified post parameter
     *
     * @param       string  $paramName      the name of the parameter
     * @param       bool    $string         is the parameter a string
     * @return      mixed   the parameter
     */
    public function getPostParameter(
        /*string*/ $paramName,
        /*bool*/ $string=true
    )
    {
        $return = null ;
        if (isset($this->_post[$paramName])) {
            $return = $this->_post[$paramName] ;
        }
        return $this->removeMagicQuotes($return, $string);
    }

    /**
     * Returns the specified file
     *
     * @param       string  $fileName       the file name
     * @return      File    the file
     */
    public function getFile(/*string*/ $fileName)
    {
        $return = null ;
        if (isset($this->_file[$fileName])
            && $this->_file[$fileName]['error'] == UPLOAD_ERR_OK
        ) {
            $return = new File(
                $this->_file[$fileName]['name'],
                $this->_file[$fileName]['tmp_name'],
                $this->_file[$fileName]['type']
            );
        }
        return $return ;
    }

    /**
     * This function returns the specified server parameter
     *
     * @param       string  $paramName      the server parameter name
     * @return      string  the server parameter
     */
    public function getServerParameter(/*string*/ $paramName)
    {
        $return = null ;
        if (isset($this->_server[$paramName])) {
            $return = $this->_server[$paramName] ;
        }
        return $return ;
    }

    /**
     * The function returns the IP address for the user that requested the page
     *
     * @return      string  the IP address
     */
    public function getIP()
    {
        return $this->_server['REMOTE_ADDR'];
    }

    /**
     * Returns the protocol of the request (http:// ou https://)
     * 
     * @return string
     */
    public function getProtocol()
    {
        if (isset($this->_server['HTTPS']) && !isNullOrEmptyString($this->_server['HTTPS'])) {
            return 'https://';
        } else {
            return 'http://';
        }
    }
    
    /**
     * The finction returns the server URI
     *
     * @return    string    the hostname
     */
    public function getHostURI()
    {
        return $this->getProtocol().
                $this->_server['HTTP_HOST'].
                $this->_server['SCRIPT_NAME'] ;
    }

    /**
     * The finction returns the URI that accessed the page
     *
     * @return    string    the request URI
     */
    public function getRequestURI()
    {
        return $this->getProtocol().
                $this->_server['HTTP_HOST'].
                $this->_server['REQUEST_URI'] ;
    }

    /**
     *
     *
     * Récupère la requête sous forme de chaine
     *
     * @return      string  the get request
     */
    public static function getGETParameters()
    {
        return stripslashes(http_build_query($_GET));
    }

    /**
     * This function must be called instead of the construtor
     *
     * @return Request instance
     */
    public static function getInstance()
    {

        if (self::$_singleton == null) {
            self::$_singleton = new Request();
        }

        return self::$_singleton;
    }

}
